if(files.cwd().endsWith("脚本")){
  let _require = require;
  require = function(path){
    if(path.startsWith("./")){
      path = path.replace("./","");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");
var time = random(2000, 3000)

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
      toastLog("检查是否有打开权限!");
      while (true) {
          var allow = text("允许").findOne(5000);
          if (allow) {
              sleep(1000)
              toastLog("发现允许授权!");
              allow.click();
              sleep(1000);
              thread.interrupt();
              break;
          }
      }
  });
  sleep(1000);
  var result = launchApp(app_name);
  sleep(3000);
  thread.interrupt();
  return result;
}


function task(){
   sleep(5000)
   //通知弹窗
   //更新弹窗
   var 立即更新 = text("立即更新").findOne(1000);
   sleep(3000)
   var 弹窗确定 = id("confirm").findOne(1000);
   if(弹窗确定){
    弹窗确定.click();
   }
   var 我的 = text("我的").findOne(1000);
    log("我的:" + 我的)
    if(我的){
        我的.click();
    }
    sleep(1000)
    var 任务大厅 = id("task_field").findOne(1000);
    if (任务大厅) {
        var finalActivity = currentActivity();
        任务大厅.click();
        for(var i = 0;i<5 ; i++){
            var 去观看 = text("去观看").findOne(1000);
              if(去观看){
                  click(random(555,575),random(330,340))
                  if(i*20 < 100){
                    utils.updateAppProgress(i*20);
                  }
                  toastLog("广告中..")
                  utils.waitAdvert(finalActivity);
              }else{
                toastLog('未找到去观看');
                return ;
              }  
              sleep(2000) 
        }
  }else{
    toastLog('任务大厅 为找到');
    return ;
  }
  log("任务已完成.")
}


function main() {
    let open_app = launchAppEnhance("蚁丛旅游");
    if (open_app) {
      task()
    }
    else {
      toastLog("蚁丛旅游开失败，请确认是否安装并授权打开!");
    }
  }
  
  try {
    main();
    utils.updateAppProgress(100);
  } catch (error) {
    toastLog(error);
  }
  
  storage.put("script_running_status", "end");